package com.example.stars_project_android.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.stars_project_android.R;
import com.example.stars_project_android.index.MyRoom;
import com.example.stars_project_android.index.Myguestbook;
import com.example.stars_project_android.personalCenter.Concernstar;
import com.example.stars_project_android.personalCenter.SetUp;
import com.example.stars_project_android.util.Constant;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 我的fragment
 */
public class UserFragment extends Fragment {

    private LinearLayout myHome;//我的房间
    private LinearLayout myStar;//我关注的星
    private LinearLayout mySay;//我的留言
    private LinearLayout mySetting;//设置
    private TextView starId;    //星星id
    private TextView starDetil;   //星星信息描述
    private TextView textTime;  //登陆时间

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_user, container, false);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setView();
//        try {
//            //设置数据
//            setData();
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        //我的房间
        myHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), MyRoom.class);
                startActivity(intent);
            }
        });
        //我关注的星
        myStar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent();
                intent2.setClass(getActivity(), Concernstar.class);
                startActivity(intent2);
            }
        });
        //我的留言
        mySay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent3 = new Intent();
                intent3.setClass(getActivity(), Myguestbook.class);
                startActivity(intent3);
            }
        });
        //设置
        mySetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent4 = new Intent();
                intent4.setClass(getActivity(), SetUp.class);
                startActivity(intent4);
            }
        });
    }

    /**
     * 设置数据
     * @throws ParseException
     */
    private void setData() throws ParseException {
        starId.setText(Constant.starName);
        starDetil.setText(Constant.starName + "是一颗位于赤经" + Constant.longitude + "，赤纬 " + Constant.latitude +"的小星\n" +
                "                    星，距离地球" + Constant.distance + "光年");
        //获取当前时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss
        Date date = new Date(System.currentTimeMillis());
        //获取注册时间
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date endDate = df.parse(Constant.registerTime);
        //计算距离现在的天数
        int timeNum = getGapCount(date,endDate);
        textTime.setText("第" + timeNum +"天");
    }

    /**
     * 计算登录的天数
     * @param startDate
     * @param endDate
     * @return
     */
    public int getGapCount(Date startDate, Date endDate) {
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.setTime(startDate);
        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
        fromCalendar.set(Calendar.MINUTE, 0);
        fromCalendar.set(Calendar.SECOND, 0);
        fromCalendar.set(Calendar.MILLISECOND, 0);

        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(endDate);
        toCalendar.set(Calendar.HOUR_OF_DAY, 0);
        toCalendar.set(Calendar.MINUTE, 0);
        toCalendar.set(Calendar.SECOND, 0);
        toCalendar.set(Calendar.MILLISECOND, 0);

        return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24));
    }

    /**
     * 绑定控件对象
     */
    private void setView() {
        //我的房间
        myHome = getActivity().findViewById(R.id.my_home);
        //我关注的星
        myStar = getActivity().findViewById(R.id.my_star);
        //我的留言
        mySay = getActivity().findViewById(R.id.my_say);
        //设置
        mySetting = getActivity().findViewById(R.id.my_setting);
        starId = getActivity().findViewById(R.id.star_id);
        starDetil = getActivity().findViewById(R.id.star_detil);
        textTime = getActivity().findViewById(R.id.text_time);
    }

}
